1.7 File and Directory Operation Commands
ls — Directory Listing Utility
Format:
ls [options] [directory_or_file]
Common Options:
-
-l: Long listing format (displays file type, permissions, ownership, size, and modification date).
-
-a: Show hidden files (those starting with a dot .).
-
-h: Display sizes in a human-readable format (e.g., KB, MB, GB); must be used with -l.
-
-t: Sort content by modification time.
-
-r: Reverse the sorting order.
-
-R: Recursively list subdirectories.
-
-i: Display the file's index node (inode) number.
-
-S: Sort content by file size.
-
-d: List directories themselves, rather than their contents.
Examples:
$ ls
$ ls -l
$ ls -lh
pwd — Print Working Directory Utility
Format:
pwd
cd — Change Directory Utility
Format:
cd [options] [directory_name]
Common Options:
-
-L: Follow symbolic links / logical path (default behavior).
-
-P: Use the physical directory structure without following symbolic links (resolves real path).
-
-e: When used with -P, returns an error status if the directory change fails.
Examples:
$ cd /home # Navigate to the home directory under root
$ cd .. # Move up one directory level
$ cd ~ # Change to the home directory of the current user
$ cd - # Switch to the previously occupied directory
mkdir — Make Directory Utility
Format:
mkdir [options] directory_name
Common Options:
-
-p: Recursively create parent directories if they do not exist.
-
-v: Verbose mode (print a message for each created directory).
-
-m: Assign specific permissions to the directory during creation.
Examples:
$ mkdir dir1
$ mkdir -p a/b/c
rmdir — Remove Empty Directory Utility
Format:
rmdir [options] directory_name
rm — Remove Files or Directories Utility
Format:
rm [options] directory_or_file
Common Options:
-
-r / -R: Recursively delete directories and their sub-contents.
-
-f: Force removal (do not prompt for confirmation).
-
-i: Prompt for confirmation before every removal.
-
-v: Verbose mode (display file names as they are deleted).
-
-d: Delete empty directories.
-
-I: Prompt once before removing more than three files or when removing recursively.
Examples:
$ rm file
$ rm -r dir
$ rm -rf dir \# Use -rf with extreme caution.
cp — Copy Files or Directories Utility
Format:
cp [options] source_file_or_directory destination_file_or_directory
Common Options:
-
-r / -R: Recursively copy directories and their sub-contents.
-
-a: Archive mode (equivalent to -dpR, preserves links, file attributes, and ownership).
-
-f: Force copy by removing existing destination files if necessary.
-
-i: Prompt for confirmation before overwriting an existing file.
-
-n: Do not overwrite an existing file.
-
-v: Verbose mode (show copy progress).
-
-u: Copy only when the source file is newer than the destination file or when the destination is missing.
-
-p: Preserve file permissions, timestamps, and ownership.
-
--preserve=ATTR: Preserve specified attributes.
-
--no-preserve=ATTR: Do not preserve certain specified attributes.
Examples:
$ cp file1 file2
$ cp -r dir1 dir2
mv — Move / Rename Utility
Format:
mv [options] source_file_or_directory destination_file_or_directory_name
Common Options:
-
-i: Prompt for confirmation before overwriting an existing file.
-
-f: Force move by overwriting existing files without prompting.
-
-n: Do not overwrite any existing file.
-
-v: Verbose mode (show progression).
-
-u: Move only when the source file is newer than the destination file or when the destination is missing.
-
-t DIR: Move all source arguments into the specified target directory DIR.
-
-T: Treat the target as a regular file instead of a directory.
Examples:
mv file /tmp